package com.gainloop.Api;

import org.json.JSONException;
import org.json.JSONObject;

public class ApiResponse
{

	final String CODE_KEY = "code";
	final String DATA_KEY = "data";
	final String ERROR_KEY = "error";
	
	
	protected int _retCode;
	protected JSONObject _dataJsonObj;
	protected String _error;

	
	public ApiResponse(int retCode, JSONObject dataJsonObj, String error)
	{
		_retCode = retCode;
		_dataJsonObj = dataJsonObj;
		_error = error;
	}
	
	public ApiResponse(JSONObject response) throws JSONException
	{
		_retCode = response.optInt(CODE_KEY);
		_dataJsonObj = response.optJSONObject(DATA_KEY);
		_error = response.optString(ERROR_KEY);
	}
	
	public int getReturnCode()
	{
		return _retCode;
	}
	
	public JSONObject getDataJsonObject()
	{
		return _dataJsonObj;
	}
	
	public String getError()
	{
		if(_error == null) {
			if(_retCode == 0)
				return "";
			
			return "Unknown Error " + _retCode;
		}
		return _error;
	}
	
}
